package com.tensquare.jwt;

import io.jsonwebtoken.*;
import org.junit.Test;

import java.util.Date;

/**
 * Created by WF on 2020/10/24 10:09
 */
public class JwtDemo {

    //1.创建token(基本方式)
    @Test
    public void test01(){
        //1.1)得到一个jwtBuilder对象
        JwtBuilder jwtBuilder =
                Jwts.builder()
                    .setId("8888")
                    .setSubject("admin")
                    .setIssuedAt(new Date())
                    .signWith(SignatureAlgorithm.HS256, "zelin");

        //1.2)得到token对象
        String token = jwtBuilder.compact();
        //1.3)输出
        System.out.println("token = " + token);

    }

    //2.jwt的解析(基本方式)
    @Test
    public void test02(){
        //2.1)定义要解析的token
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ODg4Iiwic3ViIjoiYWRtaW4iLCJpYXQiOjE2MDM1MDU2MjN9.IkVSJKuyDd0yb8i1toCqZxZFyJ73dP9STsbECz0O96Y";
        //2.2)进行解析
        Claims claims = Jwts.parser()
                            .setSigningKey("zelin")
                            .parseClaimsJws(token)
                            .getBody();
        //2.3)得到解析后的数据
        String id = claims.getId();
        String subject = claims.getSubject();
        Date issuedAt = claims.getIssuedAt();
        System.out.println("id = " + id);
        System.out.println("subject = " + subject);
        System.out.println("issuedAt = " + issuedAt);
    }

    //3.创建token（增加过期时间）
    @Test
    public void test03(){
        //3.1)得到当前系统时间的毫秒值
        long timeMillis = System.currentTimeMillis();
        Date expireDate = new Date(timeMillis + 600 * 1000);    //加10分钟
        //3.2)得到jwtBuilder对象
        JwtBuilder jwtBuilder = Jwts.builder()
                .setId("9999")
                .setIssuedAt(new Date(timeMillis))
                .setSubject("admin")
                .setExpiration(expireDate)
                .signWith(SignatureAlgorithm.HS256, "zelin");
        //3.3)得到token值
        String token = jwtBuilder.compact();
        //3.4)打印
        System.out.println("token = " + token);
    }

    //4.解析jwt（带有过期时间）
    @Test
    public void test04(){
        //4.1)定义解析的token
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI5OTk5IiwiaWF0IjoxNjAzNTA2NTQzLCJzdWIiOiJhZG1pbiIsImV4cCI6MTYwMzUwNzE0M30.Nn5P9PI9iLtst6VRq-X2aIqk9VQYCvBilAJlRvTtfDM";
        //4.2)得到claims对象
        Claims claims = Jwts.parser()
                        .setSigningKey("zelin")
                        .parseClaimsJws(token).getBody();
        //4.3)得到解析后的内容
        String id = claims.getId();
        Date issuedAt = claims.getIssuedAt();
        String subject = claims.getSubject();
        Date expiration = claims.getExpiration();
        System.out.println("id = " + id);
        System.out.println("issuedAt = " + issuedAt);
        System.out.println("subject = " + subject);
        System.out.println("expiration = " + expiration);
    }

    //5.创建token（携带数据）
    @Test
    public void test05(){
        //5.1)创建jwtbuilder对象
        JwtBuilder jwtBuilder = Jwts.builder().setId("8888").setIssuedAt(new Date()).setSubject("admin").claim("roles", "admin").signWith(SignatureAlgorithm.HS256, "zelin");
        //5.2)得到token对象
        String token = jwtBuilder.compact();
        //5.3)输出
        System.out.println("token = " + token);
    }

    //6.解析token（携带数据）
    @Test
    public void test06(){
        //6.1)解析token
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ODg4IiwiaWF0IjoxNjAzNTA3MDQwLCJzdWIiOiJhZG1pbiIsInJvbGVzIjoiYWRtaW4ifQ.dXOGIz2Btqt7PWHu8QAMQxXu98NIdjPfkb63iLDZL2A";
        Claims claims = Jwts.parser().setSigningKey("zelin").parseClaimsJws(token).getBody();
        //6.2)得到解析后的数据
        Date issuedAt = claims.getIssuedAt();
        String id = claims.getId();
        Date issuedAt1 = claims.getIssuedAt();
        String roles = (String) claims.get("roles");
        //6.3)打印输出
        System.out.println("issuedAt = " + issuedAt);
        System.out.println("id = " + id);
        System.out.println("issuedAt1 = " + issuedAt1);
        System.out.println("roles = " + roles);

    }
}
